﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Forging2.Common
{
    public class AppInitialization
    {
        public Boolean AppConfigReadied = false;    
        
        public void LoadConfig()
        {
            JsonSerializer serializer = new JsonSerializer();
            using (var sr = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\AppConfig.json"))
            {
                Const.Config = serializer.Deserialize(sr, typeof(AppConfig)) as AppConfig;
            }
            Const.Config.FurnaceWorkCells.ForEach((string s) => Const.Config.WorkCells.Add(s));
            Const.Config.ForgingWorkCells.ForEach((string s) => Const.Config.WorkCells.Add(s));
           

            Const.IGDataDbContext = new DataBase.IGDataDbContext();
            Const.ZH_IMESDbContext = new DataBase.ZH_IMES.ZH_IMESDbContext();
            List<string> equipmentgid = new List<string>();
            Const.HeatingFurnaces = new List<Heating.HeatingFurnace>();
            Const.HeatingPLCAddress = new List<bas_plcaddress_zy>();
            Const.Config.HeatingPLCAddress.ForEach(gid =>
            {
                var address = Const.IGDataDbContext.bas_plcaddress_zy.Find(gid);
                if (!equipmentgid.Contains(address.EQUIPMENT_GID))
                {
                    equipmentgid.Add(address.EQUIPMENT_GID);
                }
                Const.HeatingPLCAddress.Add(address);
                
            });
            
            foreach (var item in equipmentgid)
            {
                var basequipmentzy = Const.IGDataDbContext.bas_equipment_zy.Find(item);
                //var count = "Counts" + basequipmentzy.CODE.Substring(basequipmentzy.CODE.Length-1, 1);
                //var temperatures= "Temperatures" + basequipmentzy.CODE.Substring(basequipmentzy.CODE.Length-1, 1);
                var heatingfurnaces = new Heating.HeatingFurnace(basequipmentzy.GID)
                {
                    GID = basequipmentzy.GID,
                    CODE = basequipmentzy.CODE,
                    NAME = basequipmentzy.NAME,
                    USAGE_CATEGORY = basequipmentzy.USAGE_CATEGORY,
                    STATUS = basequipmentzy.STATUS,
                    MESCODE = basequipmentzy.WORK_CELL_CODE
                };
                Const.HeatingFurnaces.Add(heatingfurnaces);
                
            }
            //加入0号炉编号
            Const.HeatingFurnaces.Add(new Heating.HeatingFurnace( Guid.NewGuid().ToString()) { CODE = "JRL0", NAME = "0#加热炉", USAGE_CATEGORY = 2, STATUS = 0 });
            Const.Front.Notification = "配置文件载入完成！";
            AppConfigReadied = true;
        }
        public void BindingFrontData()
        {

        }

        public async void ServiceRunnig()
        {
            Const.OPCServer = new Services.OPCService.OPCServer(Const.Config.OPCServerPath);
            await Const.OPCServer.OpenOPCServer(); 
        }

        
    }
}
